from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet
from apps.goods.models import SKU, GoodsCategory, SPU, SPUSpecification
from ..serializers.sku import SKUSerializer, SKUCategorieSerializer, SPUSimpleModelSerializer, SPUSpeSerializer

from ..utils import PageNum
from rest_framework.generics import ListAPIView
from rest_framework.views import APIView
from apps.meiduo_admin.serializers.sku import SPUSpeSerializer


class SUKModelViewSet(ModelViewSet):

    # queryset = SKU.objects.all()
    # 重写get_queryset
    def get_queryset(self):
        keyword = self.request.query_params.get('keyword')
        if keyword:
            return SKU.objects.filter(name__contains=keyword)
        return SKU.objects.all()
    serializer_class = SKUSerializer
    pagination_class = PageNum


class ThreeJiCategoryView(ListAPIView):

    queryset = GoodsCategory.objects.filter(subs=None)
    serializer_class = SKUCategorieSerializer


class SPUSimpleView(ListAPIView):

    queryset = SPU.objects.all()
    serializer_class = SPUSimpleModelSerializer

# 获取商品规格
class SPUSpecView(APIView):

    def get(self, request, pk):
        spec = SPUSpecification.objects.filter(spu_id=pk)
        serializer = SPUSpeSerializer(spec, many=True)
        return Response(serializer.data)